package com.liang.blog.utils;

import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtils {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //md加密
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes("UTF-8")));
        //盐值
        System.out.println(UUID.randomUUID().toString().replace("-", ""));
    }

    //加密
    @Test
    void encrypt() throws UnsupportedEncodingException {
        String password = "123456";
        String salt = UUID.randomUUID().toString().replace("-", "");
        //md5(salt+password)
        String secretString = DigestUtils.md5DigestAsHex((salt + password).getBytes("UTF-8"));
        //数据库存储  盐值+密文
        String sqlPassword = salt + secretString;
        System.out.println(sqlPassword);
    }
    @Test
    void verity() throws UnsupportedEncodingException {
        String inputPassword = "123456";
        String sqlPassword = "09d19e5ef0ba460d93954b44640a0cba20404d0b9d25984092e4723199ecede1";

        String salt = sqlPassword.substring(0, 32);
        //md5(salt+password)
        String secretString = DigestUtils.md5DigestAsHex((salt + inputPassword).getBytes("UTF-8"));
        //数据库存储  盐值+密文
        boolean result = (salt + secretString).equals(sqlPassword);
        System.out.println(result);

    }

}
